//-------------------------------------------------------------------
//	Copyright (c) 2013-2014  Zhirnov Andrey
//	This file is part of the "UX OpwnGL Framework" project.
//	See copyright notice in "UX_GL_Framework.h".
//-------------------------------------------------------------------

#pragma once

#include <Includes.h>

namespace Framework
{

#	define CHECK	CHECK_WARNING


	//
	// Application
	//

	class UX_EXPORT CApplication : public IWindowUpdater, public IWindowEventListener
	{
	// types
	protected:
		struct EInitFlags
		{
			enum type
			{
				NONE		= 0,
				USE_OPENCL	= 1 << 0,

				DEFAULT		= USE_OPENCL,
			};
		};

		typedef AutoPtr< CLDevice >	CLDevicePtr;


	// variables
	private:
		CLogger				_logger;
		CThreadManager		_threadMngr;
		CInput				_input;
		GLDefaultRender		_render;
		GLWindow			_window;
		CLDevicePtr			_clDevice;
		CMainWindowThread	_mainThread;
		TSystemInfo			_systemInfo;

		PerfomanceTimer		_timer;
		float				_lastTime,
							_timeDelta,
							_updateTime;

		GLSubMesh			_quadVertexAttribs;
		GLBuffer			_quadVertices;


	//  methods
	private:
		void OnUpdate (CWindow *wnd) override;
		void ProcessMessage (const TWindowEvent &wndEvent) override;

		void _Init();
		void _Free();

	protected:
		bool _Run (uni_c_string resPath, const CWindow::TDescriptor &desc, const GLWindow::TGLDescriptor &glDesc, EInitFlags::type flags = EInitFlags::DEFAULT);
		bool _RunDefault();

		// helpers
		void DrawPoint();
		void DrawQuad();

	public:
		CApplication();
		virtual ~CApplication();


		GLWindow *			Window()			{ return &_window; }
		IThread *			Thread()			{ return &_mainThread; }
		CInput *			Input()				{ return &_input; }
		GLDefaultRender *	Render()			{ return &_render; }

		TSystemInfo const & SystemInfo()		{ return _systemInfo; }

		float	GetUpdateTimeDelta()	const	{ return _timeDelta; }

		
	// interface
	protected:
		virtual void Draw() = 0;
		virtual void Update() = 0;
		virtual void OnResize() = 0;
		virtual void OnInit() = 0;
		virtual void OnFree() = 0;
	};

}